package com.hzqy.service.hotel;

import com.hzqy.commons.utils.JsonUtils;
import com.hzqy.persistence.dao.hotel.HotelCouponDao;
import com.hzqy.persistence.dao.picture.PictureDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.web.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class HotelCouponServiceBean extends GenericServiceBean<HotelCouponVo, HotelCouponDao, Integer>{
    protected final Logger LOG = LoggerFactory.getLogger(getClass());
    private final String picTypeCoupon = "优惠券";

    @Resource
    private HotelCouponDao yhqDao;
    @Resource
    private PictureDao pictureDao;

    @Override
    public HotelCouponDao getEntityDao() {
        return yhqDao;
    }

    public Map<String, Object> selectYhq(HotelCouponVo yhqVo, int curPage, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        int rows = yhqDao.selectYhqCount(yhqVo);
        map.put("total", rows);
        map.put("f_hc_name", yhqVo.getF_hc_name());
        map.put("f_hc_type", yhqVo.getF_hc_type());
        map.put("f_hc_price", yhqVo.getF_hc_price());
        map.put("f_hc_instructions", yhqVo.getF_hc_instructions());
        map.put("f_hc_validity", yhqVo.getF_hc_validity());
        map.put("firstResult", (curPage-1)*pageSize);
        map.put("maxResults", pageSize);

        List<HotelCouponVo> results = yhqDao.selectYhq(map);
        map.put("rows", results);
        return map;
    }

    public int insertYhq(HotelCouponVo yhqVo,String picJson){
        yhqDao.insertYhq(yhqVo);
        int f_hc_id = yhqVo.getF_hc_id();
        List<CmPicturesVo> picList = JsonUtils.jsonToPicVo(picJson);
        for(int i=0;i<picList.size();i++) {
            CmPicturesVo cmPicturesVo = new CmPicturesVo();
            cmPicturesVo = picList.get(i);
            cmPicturesVo.setF_cp_relationid(f_hc_id);
            pictureDao.insertPic(cmPicturesVo);
        }
        return 1;
    }
    public HotelCouponVo selectYhqById(int f_hc_id) {
        HotelCouponVo yhqVo= yhqDao.selectYhqById(f_hc_id);
        return yhqVo;
    }
    public Map<String, Object> updateYhq(HotelCouponVo yhqVo,String picJson) {
        Map<String, Object> resultMap = new HashMap<>();
        int flag = yhqDao.updateYhqById(yhqVo);

        String listPicId = JsonUtils.jsonToPicIdList(picJson);
        int f_cv_relationid = yhqVo.getF_hc_id();
        String picType= picTypeCoupon;
        Map<String,Object> picMap = new HashMap<String, Object>();
        picMap.put("listPicId", listPicId);
        picMap.put("f_cp_relationid", f_cv_relationid);
        picMap.put("picType", picType);
        pictureDao.deletePic(picMap);
        List<CmPicturesVo> picList = JsonUtils.jsonToPicVo(picJson);
        for(int i=0;i<picList.size();i++) {
            CmPicturesVo cmPicturesVo = new CmPicturesVo();
            cmPicturesVo = picList.get(i);
            cmPicturesVo.setF_cp_relationid(f_cv_relationid);
            pictureDao.insertPic(cmPicturesVo);
        }

        if(flag>0){
            resultMap.put("success", true);
            resultMap.put("result","更新内容成功!");
        }else{
            resultMap.put("success", false);
            resultMap.put("result","更新内容失败!");
        }
        return resultMap;
    }

    public Map<String, Object> deleteYhqContent(int f_hc_id) {
        Map<String, Object> resultMap = new HashMap<>();
        int flag = yhqDao.deleteYhqContent(f_hc_id);
        if(flag>0){
            resultMap.put("success", true);
            resultMap.put("result","删除内容成功!");
        }else{
            resultMap.put("success", false);
            resultMap.put("result","删除内容失败!");
        }
        return resultMap;
    }

    public Map<String, Object> selectCouponCodeList(HotelCouponCodeVo hotelCouponCodeVo, int curPage, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        int rows = yhqDao.selectCouponCodeCount(hotelCouponCodeVo);
        map.put("total", rows);
        map.put("f_hcc_couponid", hotelCouponCodeVo.getF_hcc_couponid());
        map.put("f_hcc_code", hotelCouponCodeVo.getF_hcc_code());
        map.put("firstResult", (curPage-1)*pageSize);
        map.put("maxResults", pageSize);

        List<HotelCouponCodeVo> results = yhqDao.selectCouponCodeList(map);
        map.put("rows", results);
        return map;
    }

    public int insertCouponCode(HotelCouponCodeVo hotelCouponCodeVo){
        HotelCouponCodeVo vo = new HotelCouponCodeVo();
        vo.setF_hcc_code(hotelCouponCodeVo.getF_hcc_code());
        int rows = yhqDao.selectCouponCodeCount(vo);//查询是否已存在同一code的券码
        if(rows>0){
            return -1;
        }else{
            return yhqDao.insertCouponCode(hotelCouponCodeVo);
        }
    }

    public HotelCouponCodeVo selectCouponCodeById(int f_hcc_id) {
        return yhqDao.selectCouponCodeById(f_hcc_id);
    }

    public int updateCouponCode(HotelCouponCodeVo hotelCouponCodeVo) {
        HotelCouponCodeVo vo = new HotelCouponCodeVo();
        vo.setF_hcc_code(hotelCouponCodeVo.getF_hcc_code());
        int rows = yhqDao.selectCouponCodeCount(vo);//查询是否已存在同一code的券码
        if(rows>0){
            return -1;
        }else{
            return yhqDao.updateCouponCode(hotelCouponCodeVo);
        }
    }

    public Map<String, Object> deleteCouponCode(int f_hcc_id) {
        Map<String, Object> resultMap = new HashMap<>();
        int flag = yhqDao.deleteCouponCode(f_hcc_id);
        if(flag>0){
            resultMap.put("success", true);
            resultMap.put("result","删除内容成功!");
        }else{
            resultMap.put("success", false);
            resultMap.put("result","删除内容失败!");
        }
        return resultMap;
    }
}
